package com.heimali.happyreader;

import com.heimali.happyreader.config.Constants;
import com.heimali.happyreader.config.PersonalConfig;
import com.heimali.happyreader.utils.FileUtils;
import com.heimali.happyreader.utils.SystemUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SettingActivity extends BaseActivity {
//TODO 1、是否显示图片； 2、版本号
	
	private TextView usernameText;
	private TextView localCacheText;
	private TextView checkUpdateLabel;
	private TextView versionText;
	private TextView isAutoRefreshListLabel;
	private ImageView isAutoRefreshCheckbox;
	private Button logoutButton;

	private final int UPDATE_LOCAL_CACHE_SIZE = 101;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		initView();
	}

	public void onResume() {
		super.onResume();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_btn:
			finish();
			break;
		case R.id.is_auto_refresh_list_label:
			toggleAutoRefresh();
			break;
		case R.id.check_update_label:
			checkUpdate();
			break;
		case R.id.btn_logout:
			logout(SettingActivity.this);
			break;
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_LOCAL_CACHE_SIZE:
				localCacheText.setText(msg.obj.toString());
				break;
			default:
				break;
			}
		}
	};

	private void initView() {

		Button leftButton = (Button) findViewById(R.id.left_btn);
		leftButton.setVisibility(View.VISIBLE);
		leftButton.setOnClickListener(this);

		TextView titleText = (TextView) findViewById(R.id.top_title);
		titleText.setText(R.string.setting_title);

		Button rightButton = (Button) findViewById(R.id.right_btn);
		rightButton.setVisibility(View.GONE);

		usernameText = (TextView) findViewById(R.id.username_text);
		usernameText.setText(Statics.getUsername());
		localCacheText = (TextView) findViewById(R.id.local_cache_text);
		versionText = (TextView) findViewById(R.id.version_text);
		
		isAutoRefreshCheckbox = (ImageView) findViewById(R.id.is_auto_refresh_list_checkbox);
		isAutoRefreshListLabel = (TextView) findViewById(R.id.is_auto_refresh_list_label);
		isAutoRefreshListLabel.setOnClickListener(this);
		checkUpdateLabel = (TextView) findViewById(R.id.check_update_label);
		checkUpdateLabel.setOnClickListener(this);
		logoutButton = (Button) findViewById(R.id.btn_logout);
		logoutButton.setOnClickListener(this);

		initAutoRefresh();
		updateLocalCacheSize();
		updateVersion();
	}

	private void updateVersion() {
		versionText.setText(SystemUtils.getVersionName());
	}
	
	private void updateLocalCacheSize() {
		new Thread(new Runnable() {
			public void run() {
				String size = FileUtils.getNodeSize(Constants.LOCAL_CACHE_PATH);
				handler.obtainMessage(UPDATE_LOCAL_CACHE_SIZE, size).sendToTarget();
			}
		}).start();
	}

	private void initAutoRefresh() {
		boolean isAutoRefresh = PersonalConfig.getInstance()
				.getConfig(Constants.PERSONAL_CONFIG_IS_AUTO_REFRESH, false);
		isAutoRefreshCheckbox.setImageResource(isAutoRefresh ? R.drawable.checkon : R.drawable.check);
	}

	private void toggleAutoRefresh() {
		boolean isAutoRefresh = !PersonalConfig.getInstance().getConfig(Constants.PERSONAL_CONFIG_IS_AUTO_REFRESH,
				false);
		isAutoRefreshCheckbox.setImageResource(isAutoRefresh ? R.drawable.checkon : R.drawable.check);
		PersonalConfig.getInstance().setConfig(Constants.PERSONAL_CONFIG_IS_AUTO_REFRESH, isAutoRefresh);
	}

}
